<?php

namespace Home\Controller;

class BordController extends CommController
{
    public function index()
    {

        $this->assign("info", $this->uinfos);
        $this->assign("dis", $this->isbn["dis_isbn"]);
        $this->display();
    }

    public function useraction($id)
    {
        $salestaff = M("salestaff");
        $salestaff->create();
        if ($salestaff->where("ssa_id=" . $id)->save()) {
            $this->redirect("Bord/index");
        } else {
            $this->redirect("Bord/index");
        }
    }

    /**
     * 销售码报告
     * @param string $start
     * @param string $end
     * @return response
     */
    public function report($start = null, $end = null)
    {
        /**
         * 销售码数据
         * array(14) {
                      ["dis_id"] => string(2) "31"
                      ["dis_ssaid"] => string(2) "39"
                      ["dis_isbn"] => string(10) "2101401191"
                      ["dis_custype"] => string(2) "21"
                      ["dis_preid"] => string(1) "1"
                      ["dis_timecode"] => string(6) "191104"
                      ["dis_sn"] => string(2) "01"
                      ["dis_endtimecode"] => string(6) "201104"
                      ["dis_limit"] => string(1) "0"
                      ["dis_limitcode"] => string(1) "2"
                      ["dis_tag"] => NULL
                      ["dis_status"] => string(1) "1"
                      ["dis_time"] => string(10) "1572882410"
                      ["dis_ssa_uple"] => NULL
                    }
         */

        if(empty($this->isbn)){
            $this->display();
            return;
        }

        $cash = M("cash");

        $where["c_disid"] = $this->isbn["dis_id"];
        $start != null && $end != null ? $where["c_time"] = array("between", array(strtotime($start), strtotime($end))) : null;
        $list = getpages($cash, $where, 10, "c_time desc");

        //推广人员信息
        $dis_ssaid = empty($this->isbn['dis_ssaid'])?0:$this->isbn['dis_ssaid'];
        $user = M('salestaff')->where("'ssa_id' = {$dis_ssaid}")->find();

        if ($user['ssa_agid'] == $this->uinfos['ssa_id']) {
            //一级代理
            $this->assign("total_amount", $cash->where('c_agent = ' . $user['ssa_agid'])->sum('c_agent_in'));
            $this->assign("unwithdrawal_amount", $cash->where('c_agent = ' . $user['ssa_agid'] . ' and c_status = 1')->sum('c_agent_in'));
            $this->assign("withdrawal_amount", $cash->where('c_agent = ' . $user['ssa_agid'] . ' and c_status != 1')->sum('c_agent_in'));

        //推广代理
        } else {
            //二级代理
            $this->assign("total_amount", $cash->where('c_disid = ' . $this->isbn['dis_id'])->sum('c_in'));
            $this->assign("unwithdrawal_amount", $cash->where('c_disid = ' . $this->isbn['dis_id'] . ' and c_status = 1')->sum('c_in'));
            $this->assign("withdrawal_amount", $cash->where('c_disid = ' . $this->isbn['dis_id'] . ' and c_status != 1')->sum('c_in'));

        }
        //推广APP会员
        $query = M('order', 't_vip_')->join('ana_cash ON t_vip_order.id = ana_cash.c_order_id', 'right');
        $condition['c_disid'] = $this->isbn['dis_id'];
        $orders = $query->where($condition)->order('c_order_id desc')->select();

        $this->assign("s", $start);
        $this->assign("orders", $orders);
        $this->assign("e", $end);
        $this->assign("list", $list);
        $this->display();
    }

    public function cash()
    {
        $cash = M("cash");
        $wy = D("Cashs");
        $where = $wy->gettypes($this->uinfos, $this->isbn, $this->ic_u_type, 1);
        $list = getpages($cash, $where, 10, "c_time desc");

        $this->assign("count", $cash->where($where)->count());
        $this->assign("gold", $cash->where($where)->sum("c_in"));
        $this->assign("list", $list);
        $this->display();
    }

    public function cash_pre()
    {
        $cash = M("cash");

        if ($this->ic_u_type == 21 || $this->ic_u_type == 23) {
            $where["c_disid"] = $this->isbn["dis_id"];
        }
        if ($this->ic_u_type == 2301 || $this->ic_u_type == 2302) {
            $where["c_agent"] = $this->uinfos["sag_id"];
        }
        $where["c_status"] = 1;
        $list = getpages($cash, $where, 10, "c_time desc");

        $this->assign("count", $cash->where($where)->count());
        $this->assign("gold", $cash->where($where)->sum("c_in"));
        $this->assign("list", $list);
        $this->display();
    }

    public function cash_firm()
    {
        $cash = M("cash");
        $wy = D("Cashs");

        $where = $wy->gettypes($this->uinfos, $this->isbn, $this->ic_u_type, 0);
        $list = getpages($cash, $where, 10, "c_time desc");

        $this->assign("count", $cash->where($where)->count());
        $this->assign("gold", $cash->where($where)->sum("c_in"));
        $this->assign("list", $list);
        $this->display();
    }
}
